object HexunDayMin5{
  def getDayfromHexun(ids:Seq[String],t:Int=15)={
    val ret1 = new scala.collection.mutable.ArrayBuffer[String]
    val id_str=ids.map(id=>(if(id>"sz000") "2_" else "1_")+id.substring(2)).mkString(",")
    for(times <- 1 to t;if(ret1.size==0)){
    try{
      val url=new java.net.URL("http://flashquote.stock.hexun.com/Stock_Combo.ASPX?mc="+id_str+"&dt=")
      val urlCon = url.openConnection()
      urlCon.setConnectTimeout(3000)
      urlCon.setReadTimeout( 1000 )
      val tt=io.Source.fromInputStream(urlCon.getInputStream)
      ret1 ++= tt.getLines
      tt.close
      //println(times)
    }catch{
        case a:Exception=>if(times==t)println(ids.mkString(","))
    }}
    val rr=if(ret1.size>0){
      ids.zip(ret1(0).split("\\);").map{line=>
        if(line.contains("--")) "" else {
          val dd=line.substring(line.indexOf("[")+2,line.indexOf("]")).replace("'","").split(",")
          Seq(dd(0),(dd(2).toDouble*1000).toLong,(dd(3).toDouble*1000).toLong,(dd(4).toDouble*1000).toLong,(dd(5).toDouble*1000).toLong,
          (dd(26).toDouble*100).toLong,(dd(27).toDouble*1000).toLong,(dd(1).toDouble*1000).toLong,
          (dd(32).toDouble*100).toLong,(dd(33).toDouble*100).toLong,dd(31).toDouble*10000 toLong).mkString(",")
        }
      })
    } else ids.map((_,""))
    rr.toMap
  }
  lazy val dateFormat=new java.text.SimpleDateFormat("yyyyMMddHHmmss")
  def getDayHexun(date:Long,date_sina:Long,stocks1:Seq[String],size:Int=10){
    val t=java.lang.System.currentTimeMillis
  	println(date)
    val date1=dateFormat.parse(date.toString).getTime+8000
    var f=false
    var stocks=stocks1
    if(date1>t)Thread.sleep(date1-t)
    for(i <- 1 to size;if(!f)){
      val t=java.lang.System.currentTimeMillis
      val r=getDayfromHexun(stocks).filter(a=>a._2.size>0)
      stocks=r.filter(_._2.take(8+6).toLong<(date)).toList.map(_._1)
      f=(stocks.size==0)
      println(i)
      val rr=if(i==size) r else r.filter(_._2.take(8+6).toLong>=date)
      rr.foreach(a=>save("HexunDayMin5/"+date_sina.toString.take(8)+"/"+a._1+".txt",Seq(date_sina+","+a._2),true))
      val t1=java.lang.System.currentTimeMillis
      if(!f && (date1+3000*i>t1))Thread.sleep(date1+3000*i-t1)
    }
  }
  def save(file:String,s:TraversableOnce[String],append:Boolean=false){
    val pdir=new java.io.File(file).getParentFile
    if(pdir!=null && !pdir.exists) pdir.mkdirs
    val filestream=new java.io.FileOutputStream(file,append)
    val writer1 = new java.io.OutputStreamWriter(filestream,"UTF-8")
    val writer = new java.io.PrintWriter(writer1)
    s.foreach(writer.println)
    writer.close
    writer1.close
    filestream.close
  }
  def main(args: Array[String]) {
  	val stocks="sz000758,sz000937,sz000401,sz000629,sh600729,sh600022,sz000807,sz000783,sz000793,sz002006,sz000970,sz002106,sz000100,sz000968,sz000009,sz002490,sz000792,sh600988,sz002233,sh600470,sz000691,sh600172,sz002145,sh600200,sh600885,sh600332,sh600784,sz300175,sz000007,sh600613,sz300146,sz000779,sh600520,sz002298,sh600160,sh600478,sz000030,sh600209,sz000663,sh600193,sh600466,sz002450,sh600766,sh600179,sh600381,sz000995,sz002236,sz000750,sh600300,sh600260,sz002225,sh600636,sz002456,sh600077,sh600389,sh600111,sh600645,sh600180,sz000056,sh600882,sz300104,sz002568,sz300162,sz000799,sz002070,sh600228,sh600113,sh600010,sh600696,sz000975,sh600340,sz300205,sz000752".split(",")
    val date=new java.util.Date
    val now=dateFormat.format(date)
    def da(a:Int)=now.take(8).toLong*1000000+(a*100)
    val datelist=(935.to(1500,5).filter(a=>a%100<60&&(a<1130||a>1300)).map(a=>(da(a),da(a))):+
      (da(1130),da(1300)):+(da(1155),da(1300)):+(da(1506),da(1500))).toList.sortBy(_._1)
    for(dd <- datelist.filter(_._1 > now.toLong)){
      getDayHexun(dd._1,dd._2,stocks)
    }
  }
}